package yw.nmh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import yw.nmh.dicInfo.domain.DicInfo;
import yw.nmh.dicInfo.dto.DicInfoDTO;
import yw.nmh.dicInfo.dto.DicUpdateStatusDTO;
import yw.nmh.mapper.DicInfoMapper;
import yw.nmh.result.R;
import yw.nmh.service.DicInfoService;

import java.util.ArrayList;
import java.util.List;

@Service
public class DicInfoServiceImpl extends ServiceImpl<DicInfoMapper,DicInfo>
        implements DicInfoService{

    @Resource
    private DicInfoService dicInfoService;

    @Resource
    private DicInfoMapper dicInfoMapper;

    @Override
    public List<String> DicNames() {
        List<DicInfo> list = dicInfoService.list();
        ArrayList<String> strings = new ArrayList<>();
        for (DicInfo dic : list) {
            strings.add(dic.getName());
        }
        return strings;
    }

    @Override
    public Boolean addDic(DicInfoDTO dicInfoDTO) {
        try {
            LambdaQueryWrapper<DicInfo> dicInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            dicInfoLambdaQueryWrapper.eq(DicInfo::getName,dicInfoDTO.getName());
            DicInfo one = this.getOne(dicInfoLambdaQueryWrapper);
            if (one == null) {
                DicInfo dicInfo = new DicInfo();
                dicInfo.setName(dicInfoDTO.getName());
                dicInfoDTO.setStatus(dicInfoDTO.getStatus());
                dicInfoMapper.insert(dicInfo);
                return true;
            }else {
                return false;
            }
        } catch (Exception e) {
            return false;
        }

    }

    @Override
    public Boolean updateStatus(DicUpdateStatusDTO dicUpdateStatusDTO) {
        try {
            DicInfo dic = dicInfoMapper.selectById(dicUpdateStatusDTO.getId());
            dic.setStatus(dicUpdateStatusDTO.getStatus());
            dicInfoMapper.updateById(dic);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
